﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.IO;
using System.Net;
using System.Threading;

namespace SecuritiesAnalysis.ToolkitWinForm
{
    public partial class frmHistoryPrice : Form
    {
        List<T_HistoryPrice> hisList;
        List<T_StockCode> codeList;
        SecuritiesAnalysisDataContext ctx;

        List<string> RowsList;

        double p;
        string strFrom;
        string strTo;

        public frmHistoryPrice()
        {
            InitializeComponent();
            ctx = new SecuritiesAnalysisDataContext();
        }

        private void btnGetHistory_Click(object sender, EventArgs e)
        {
            strFrom = this.dateFrom.Value.ToShortDateString();
            strTo = this.dateTo.Value.ToShortDateString();

            codeList = ctx.T_StockCodes.Where(o => o.Enabled==true).ToList();
            p = 0;

            //DownloadData();

            Thread newThread = new Thread(new ThreadStart(Run));
            newThread.Start();
        }

        private void Run()
        {
            foreach (T_StockCode c in codeList)
            {
                try
                {
                    InsertHistoryPrice(c.StockCode);
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.Message);
                    return;
                }

                MethodInvoker mi = new MethodInvoker(UpdateProgress);
                Invoke(mi);

                System.Threading.Thread.Sleep((int)(this.numSleep.Value));
            }
            
            MessageBox.Show("OKK");
        }   

        /// <summary>
        /// 获取股票历史数据并存入数据库
        /// </summary>
        /// <param name="code">股票代码数字</param>
        private void InsertHistoryPrice(int code)
        {
            string sSource = HistoryPriceOperator.MatchMainDIV(HistoryPriceOperator.GetHistorybyHTTP(HistoryPriceOperator.BuildCodeString(code),strFrom,strTo));

            RowsList = HistoryPriceOperator.GetRows(sSource);
            hisList = new List<T_HistoryPrice>();

            foreach (string row in RowsList)
            {
                hisList.Add(HistoryPriceOperator.CreateModel(code, row));
            }

            ctx.T_HistoryPrices.InsertAllOnSubmit(hisList);
            ctx.SubmitChanges();
        }

        /// <summary>
        /// 更新进度状态
        /// </summary>
        private void UpdateProgress()
        {
            if (p < 100)
            {
                p += 100.00 / (double)codeList.Count;
            }

            this.progressBar1.Value = (int)p;
        }

        private void frmHistoryPrice_Load(object sender, EventArgs e)
        {
            //获取 历史数据的最新日期
            T_HistoryPrice latest = ctx.T_HistoryPrices.OrderByDescending(o => o.Date).First();
            this.dateFrom.Value = latest.Date.AddDays(1);
            this.dateTo.Value = DateTime.Today;
        }

    }
}
